package com.yuwubao.uav.tool.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yuwubao.framework.bean.JsonResult;
import com.yuwubao.framework.bean.PageData;
import com.yuwubao.framework.loginFilter.LoginRequired;
import com.yuwubao.uav.tool.service.QuestionnaireService;
import com.yuwubao.uav.tool.util.StringUtil;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.Min;

/**
 * 问卷管理
 */
@RestController
@RequestMapping("/bk")
public class QuestionnaireController {

    private static final Logger logger = LogManager.getLogger(QuestionnaireController.class);
    
    @Resource
    private QuestionnaireService questionnaireService;


    @LoginRequired
    @PostMapping("/addQuestionnaire")
    @ApiOperation(value = "新增问卷")
    public JsonResult addQuestionnaire(
            @ApiParam(value = "问卷标题", required = true) @RequestParam("name") String name,
            @ApiParam(value = "问卷描述") @RequestParam(value = "description",required = false) String description
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("name", name);
            pageData.put("description", description);

            questionnaireService.addQuestionnaire(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @PostMapping("/updateQuestionnaire")
    @ApiOperation(value = "更新问卷")
    public JsonResult updateQuestionnaire(
            @ApiParam(value = "问卷ID", required = true) @RequestParam("id") String id,
            @ApiParam(value = "问卷标题", required = true) @RequestParam("name") String name,
            @ApiParam(value = "问卷描述") @RequestParam(value = "description",required = false) String description
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("id", id);
            pageData.put("name", name);
            pageData.put("description", description);
            questionnaireService.updateQuestionnaire(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/deleteQuestionnaire")
    @ApiOperation(value = "删除问卷")
    public JsonResult deleteQuestionnaire(
            @ApiParam(value = "问卷ID，多个用逗号分隔", required = true) @RequestParam("ids") String ids
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("ids", ids);

            questionnaireService.deleteQuestionnaire(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/disableOrEnableQuestionnaire")
    @ApiOperation(value = "禁用或启用问卷")
    public JsonResult disableOrEnableQuestionnaire(
            @ApiParam(value = "问卷ID，多个用逗号分隔", required = true) @RequestParam("ids") String ids,
            @ApiParam(value = "0:禁用  1：启用", required = true) @RequestParam("state") String state
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("ids", ids);
            pageData.put("state", state);

            questionnaireService.disableOrEnableQuestionnaire(pageData);
            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @GetMapping("/listQuestionnaire")
    @ApiOperation(value = "获取问卷列表")
    public JsonResult listQuestionnaire(
            @ApiParam(value = "当前页", required = true) @Min(1) @Length(max = 8) @NotEmpty @RequestParam("page") String page,
            @ApiParam(value = "当前页数量", required = true) @Min(10) @Length(max = 3) @NotEmpty @RequestParam("pageSize") String pageSize,
            @ApiParam(value = "搜索关键字") @RequestParam(value = "searchKey",required = false) String searchKey
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("searchKey", searchKey);
            pageData.put("begin", (Integer.valueOf(page) - 1) * Integer.valueOf(pageSize));
            pageData.put("pageSize", Integer.valueOf(pageSize));

            result.setResult(questionnaireService.listQuestionnaire(pageData));
            result.setResultNum(questionnaireService.listQuestionnaireCount(pageData));

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    //问卷题目类型  =========================================

    @LoginRequired
    @PostMapping("/addQuestionnaireSubType")
    @ApiOperation(value = "新增问卷题目类型")
    public JsonResult addQuestionnaireSubType(
            @ApiParam(value = "问卷题目类型", required = true) @RequestParam("names") String names,
            @ApiParam(value = "问卷题目类型备注") @RequestParam(value = "remark",required = false) String remark
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("names", names);
            pageData.put("remark", remark);

            questionnaireService.addQuestionnaireSubType(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/updateQuestionnaireSubType")
    @ApiOperation(value = "更新问卷题目类型")
    public JsonResult updateQuestionnaireSubType(
            @ApiParam(value = "问卷题目类型ID", required = true) @RequestParam("id") String id,
            @ApiParam(value = "问卷题目类型名称", required = true) @RequestParam("name") String name,
            @ApiParam(value = "问卷题目类型备注") @RequestParam(value = "remark",required = false) String remark
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("id", id);
            pageData.put("name", name);
            pageData.put("remark", remark);

            questionnaireService.updateQuestionnaireSubType(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/deleteQuestionnaireSubType")
    @ApiOperation(value = "删除问卷题目类型")
    public JsonResult deleteQuestionnaireSubType(
            @ApiParam(value = "问卷题目类型ID，多个用逗号分隔", required = true) @RequestParam("ids") String ids
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("ids", ids);
            questionnaireService.deleteQuestionnaireSubType(pageData);
            result.setSuccessful();
        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @PostMapping("/disableOrEnableQuestionnaireSubType")
    @ApiOperation(value = "禁用或启用问卷题目类型")
    public JsonResult disableOrEnableQuestionnaireSubType(
            @ApiParam(value = "ID，多个用逗号分隔", required = true) @RequestParam("ids") String ids,
            @ApiParam(value = "0:禁用  1：启用", required = true) @RequestParam("state") String state
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("ids", ids);
            pageData.put("state", state);

            questionnaireService.disableOrEnableQuestionnaireSubType(pageData);
            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @GetMapping("/listQuestionnaireSubType")
    @ApiOperation(value = "获取问卷题目类型列表")
    public JsonResult listQuestionnaireSubType(
            @ApiParam(value = "当前页", required = true) @Min(1) @Length(max = 8) @NotEmpty @RequestParam("page") String page,
            @ApiParam(value = "当前页数量", required = true) @Min(10) @Length(max = 3) @NotEmpty @RequestParam("pageSize") String pageSize,
            @ApiParam(value = "搜索关键字") @RequestParam(value = "searchKey",required = false) String searchKey
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("searchKey", searchKey);
            pageData.put("begin", (Integer.valueOf(page) - 1) * Integer.valueOf(pageSize));
            pageData.put("pageSize", Integer.valueOf(pageSize));

            result.setResult(questionnaireService.listQuestionnaireSubType(pageData));
            result.setResultNum(questionnaireService.listQuestionnaireSubTypeCount(pageData));

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @GetMapping("/listQuestionnaireSubTypeAll")
    @ApiOperation(value = "获取问卷题目类型列表所有")
    public JsonResult listQuestionnaireSubTypeAll(
            @ApiParam(value = "搜索关键字") @RequestParam(value = "searchKey",required = false) String searchKey
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("searchKey", searchKey);

            result.setResult(questionnaireService.listQuestionnaireSubTypeAll(pageData));

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    // 问卷题目 ==========================================

    @LoginRequired
    @PostMapping("/addQuestionnaireSubject")
    @ApiOperation(value = "新增问卷题目")
    public JsonResult addQuestionnaireSubject(
            @ApiParam(value = "问卷ID", required = true) @RequestParam("qId") String qId,
            @ApiParam(value = "问卷题目类型ID", required = true) @RequestParam("qTypeId") String qTypeId,
            @ApiParam(value = "问卷题目", required = true) @RequestParam("name") String name,
            @ApiParam(value = "是否是必填题目 0：不是 1：是", required = true) @RequestParam("isAnswerRequired") String isAnswerRequired,
            @ApiParam(value = "题目序号",required = true) @RequestParam(value = "orderNumber") String orderNumber
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("qId", qId);
            pageData.put("qTypeId", qTypeId);
            pageData.put("name", name);
            pageData.put("isAnswerRequired", isAnswerRequired);
            pageData.put("orderNumber", orderNumber);

            questionnaireService.addQuestionnaireSubject(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/updateQuestionnaireSubject")
    @ApiOperation(value = "更新问卷题目")
    public JsonResult updateQuestionnaireSubject(
            @ApiParam(value = "问卷题目ID", required = true) @RequestParam("id") String id,
            @ApiParam(value = "是否是必填题目 0：不是 1：是", required = true) @RequestParam("isAnswerRequired") String isAnswerRequired,
            @ApiParam(value = "问卷ID", required = true) @RequestParam("qId") String qId,
            @ApiParam(value = "问卷题目类型ID", required = true) @RequestParam("qTypeId") String qTypeId,
            @ApiParam(value = "问卷题目", required = true) @RequestParam("name") String name,
            @ApiParam(value = "题目序号",required = true) @RequestParam(value = "orderNumber") String orderNumber
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("id", id);
            pageData.put("isAnswerRequired", isAnswerRequired);
            pageData.put("qId", qId);
            pageData.put("qTypeId", qTypeId);
            pageData.put("name", name);
            pageData.put("orderNumber", orderNumber);

            questionnaireService.updateQuestionnaireSubject(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/deleteQuestionnaireSubject")
    @ApiOperation(value = "删除问卷题目")
    public JsonResult deleteQuestionnaireSubject(
            @ApiParam(value = "问卷题目ID，多个用逗号分隔", required = true) @RequestParam("ids") String ids
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("ids", ids);
            questionnaireService.deleteQuestionnaireSubject(pageData);
            result.setSuccessful();
        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

//    @LoginRequired
//    @PostMapping("/disableOrEnableQuestionnaireSubject")
//    @ApiOperation(value = "禁用或启用问卷题目")
//    public JsonResult disableOrEnableQuestionnaireSubject(
//            @ApiParam(value = "ID，多个用逗号分隔", required = true) @RequestParam("ids") String ids,
//            @ApiParam(value = "0:禁用  1：启用", required = true) @RequestParam("state") String state
//    ) {
//        JsonResult result = new JsonResult();
//        try {
//            PageData pageData = new PageData();
//            pageData.put("ids", ids);
//            pageData.put("state", state);
//
//            questionnaireService.disableOrEnableQuestionnaireSubject(pageData);
//            result.setSuccessful();
//
//        } catch (Exception e) {
//            logger.error("", e);
//            result.setFailure(e.getMessage());
//        }
//
//        return result;
//    }

    @LoginRequired
    @GetMapping("/listQuestionnaireSubjectAll")
    @ApiOperation(value = "获取问卷所有题目列表")
    public JsonResult listQuestionnaireSubjectAll(
            @ApiParam(value = "问卷ID",required = true) @RequestParam(value = "qId") String qId
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("qId", qId);

            result.setResult(questionnaireService.listQuestionnaireSubjectAll(pageData));

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }



    //问卷题目选项管理  ====================================

    @LoginRequired
    @PostMapping("/addQuestionnaireOption")
    @ApiOperation(value = "新增问卷题目选项")
    public JsonResult addQuestionnaireOption(
            @ApiParam(value = "问卷题目ID", required = true) @RequestParam("subId") String subId,
            @ApiParam(value = "问卷题目选项描述", required = true) @RequestParam("name") String name,
            @ApiParam(value = "问卷题目选项标号 A B C D") @RequestParam(value = "code",required = false) String code
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("subId", subId);
            pageData.put("name", name);
            pageData.put("code", code);

            questionnaireService.addQuestionnaireOption(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/updateQuestionnaireOption")
    @ApiOperation(value = "更新问卷题目选项")
    public JsonResult updateQuestionnaireOption(
            @ApiParam(value = "问卷题目选项ID", required = true) @RequestParam("id") String id,
            @ApiParam(value = "问卷题目ID", required = true) @RequestParam("subId") String subId,
            @ApiParam(value = "问卷题目选项描述", required = true) @RequestParam("name") String name,
            @ApiParam(value = "问卷题目选项标号 A B C D") @RequestParam(value = "code",required = false) String code
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("id", id);
            pageData.put("subId", subId);
            pageData.put("name", name);
            pageData.put("code", code);

            questionnaireService.updateQuestionnaireOption(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/deleteQuestionnaireOption")
    @ApiOperation(value = "删除问卷题目选项")
    public JsonResult deleteQuestionnaireOption(
            @ApiParam(value = "问卷题目选项ID，多个用逗号分隔", required = true) @RequestParam("ids") String ids
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("ids", ids);
            questionnaireService.deleteQuestionnaireOption(pageData);
            result.setSuccessful();
        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

//    @LoginRequired
//    @PostMapping("/disableOrEnableQuestionnaireOption")
//    @ApiOperation(value = "禁用或启用问卷题目选项")
//    public JsonResult disableOrEnableQuestionnaireOption(
//            @ApiParam(value = "ID，多个用逗号分隔", required = true) @RequestParam("ids") String ids,
//            @ApiParam(value = "0:禁用  1：启用", required = true) @RequestParam("state") String state
//    ) {
//        JsonResult result = new JsonResult();
//        try {
//            PageData pageData = new PageData();
//            pageData.put("ids", ids);
//            pageData.put("state", state);
//
//            questionnaireService.disableOrEnableQuestionnaireOption(pageData);
//            result.setSuccessful();
//
//        } catch (Exception e) {
//            logger.error("", e);
//            result.setFailure(e.getMessage());
//        }
//
//        return result;
//    }

    @LoginRequired
    @GetMapping("/listQuestionnaireOptionAll")
    @ApiOperation(value = "获取问卷题目所有选项")
    public JsonResult listQuestionnaireOptionAll(
            @ApiParam(value = "问卷题目ID",required = true) @RequestParam(value = "subId") String subId
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("subId", subId);

            result.setResult(questionnaireService.listQuestionnaireOptionAll(pageData));

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @PostMapping("/addSubjectForQuestionnaire")
    @ApiOperation(value = "给问卷新增题目")
    public JsonResult addSubjectForQuestionnaire(
            @ApiParam(value = "问卷ID",required = true) @RequestParam(value = "qId") String qId,
            @ApiParam(value = "问卷题目类型ID", required = true) @RequestParam("qTypeId") String qTypeId,
            @ApiParam(value = "问卷题目", required = true) @RequestParam("name") String name,
            @ApiParam(value = "是否是必填题目 0：不是 1：是", required = true) @RequestParam("isAnswerRequired") String isAnswerRequired,
            @ApiParam(value = "题目序号",required = true) @RequestParam(value = "orderNumber") String orderNumber,
            @ApiParam(value = "选项json数组") @RequestParam(value = "options") String options
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("qId", qId);
            pageData.put("qTypeId", qTypeId);
            pageData.put("name", name);
            pageData.put("isAnswerRequired", isAnswerRequired);
            pageData.put("orderNumber", orderNumber);

            //新建题库 返回题目ID sId
            questionnaireService.addSubjectForQuestionnaire(pageData);

            //新建选项
            if (StringUtil.isNotNvlOrBlank(options)) {
                JSONArray optionArr = JSONArray.parseArray(options);
                if (optionArr != null && optionArr.size()>0) {
                    JSONObject optionJson = null;
                    String order = "";
                    String content = "";
                    for (Object option:optionArr) {
                        optionJson = (JSONObject) option;
                        order = optionJson.get("order") == null ? "" : optionJson.get("order").toString();
                        content = optionJson.get("content") == null ? "" : optionJson.get("content").toString();
                        if (StringUtil.isNotNvlOrBlank(order) && StringUtil.isNotNvlOrBlank(content)) {
                            pageData.put("order",order);
                            pageData.put("content",content);
                            questionnaireService.addQuestionnaireOption1(pageData);
                        }
                    }
                }
            }

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }




}
